package com.zjj.lbw.designpattern.prototype.clone;

/**
 * @author zhanglei.zjj
 * @description 克隆之浅拷贝
 * @date 2023/7/14 15:55
 */
public class CloneTest1 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Student student = new Student();
        student.setAge(24);
        student.setName("zjj");
        Student student2 = (Student) student.clone();

        System.out.println("Age:" + student2.getAge() + " " + "Name:" + student2.getName());
        System.out.println("---------------------");
        student2.setAge(23);

        student2.setName("子坚君");
        System.out.println(student.getAge());
        System.out.println(student2.getAge());
        System.out.println(student.getName());
        System.out.println(student2.getName());
    }
}

/**
 * 克隆的对象必须实现Cloneable这个接口，而且需要重写clone方法
 */
class Student implements Cloneable {
    private int age;

    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        Object object = super.clone();
        return object;
    }
}